<template>
  <div>
    <el-dialog
      title="分组管理"
      :visible.sync="show"
      width="840px"
      :modal-append-to-body="false"
    >
      <div class="container">
        <div class="left-box">
          <div class="left-wrapper f1 scorll-box">
            <div
              v-for="item in leftList"
              :key="item.id"
              class="item flex-c align-center pointer nowrap"
              :class="{ active: active === item.id }"
              @click="active = item.id"
            >
              {{ item.groupName }}
            </div>
          </div>
          <div class="flex-c link pointer" @click="$refs.addGroupModal.open()">
            +新建分组
          </div>
        </div>
        <div class="right-box">
          <div class="table-box">
            <el-table
              ref="mainTable"
              :data="tableData"
              tooltip-effect="light"
              style="width: 100%"
              size="mini"
              header-row-class-name="mao-table-head"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column label="头像">
                <template slot-scope="scope">
                  <img
                    :src="scope.row.avatar"
                    crossorigin="anonymous"
                    alt=""
                    class="avatar"
                  >
                </template>
              </el-table-column>
              <el-table-column prop="accountName" label="昵称" />
              <el-table-column label="平台">
                <template slot-scope="scope">
                  {{ platformMap[scope.row.platform] }}
                </template>
              </el-table-column>
              <el-table-column prop="groupId_dictText" label="分组" />
              <el-table-column prop="memo" label="备注" />
              <el-table-column prop="todo" label="操作">
                <template slot-scope="scope">
                  <el-popover
                    v-model="showPop"
                    placement="right"
                    width="120"
                    trigger="manual"
                  >
                    <div
                      class="mb pointer popover-name"
                      style="margin-bottom: 10px"
                      @click="
                        showPop = false;
                        handleGroup(scope.row);
                      "
                    >
                      编辑分组
                    </div>
                    <div
                      class="mb pointer popover-name"
                      @click="
                        showPop = false;
                        handleRemark(scope.row);
                      "
                    >
                      编辑备注
                    </div>
                    <el-button
                      slot="reference"
                      size="mini"
                      @click.stop="showPop = !showPop"
                    >编辑</el-button>
                  </el-popover>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <div
        class="flex-b align-center"
        style="
          padding-top: 10px;
          border-top: 1px solid rgba(128, 128, 128, 0.2);
        "
      >
        <!-- <el-pagination
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        /> -->
        <div />
        <el-button type="primary" @click="toBatch">批量设置</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="编辑分组"
      :visible.sync="showGroup"
      width="290px"
      :modal-append-to-body="false"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="90px"
      >
        <el-form-item :label="isBatch ? '批量转移至' : '转移至'" prop="group">
          <el-select
            v-model="ruleForm.groupId"
            placeholder="请选择"
            class="white"
          >
            <el-option
              v-for="item in groupList"
              :key="item.id"
              :label="item.groupName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div class="flex-e align-center mt">
        <el-button size="mini" @click="showGroup = false">取消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="putIntoGroup(ruleForm.groupId)"
        >确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="修改备注"
      :visible.sync="showRemark"
      width="290px"
      :modal-append-to-body="false"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="90px"
      >
        <el-form-item label="备注" prop="memo">
          <el-input v-model="ruleForm.memo" class="white" />
        </el-form-item>
      </el-form>
      <div class="flex-e align-center mt">
        <el-button size="mini" @click="showRemark = false">取消</el-button>
        <el-button
          size="mini"
          type="primary"
          @click="changeMemo"
        >确定</el-button>
      </div>
    </el-dialog>
    <AddGroupModal ref="addGroupModal" @get="addGroup" />
  </div>
</template>
<script>
import { getGroupList, addGroup, putIntoGroup, changeMemo } from '@/api/group'

import { getList } from '@/api/auth'

import AddGroupModal from './AddGroupModal.vue'
export default {
  components: { AddGroupModal },
  data() {
    return {
      show: false,
      isBatch: false,
      showPop: false,
      showGroup: false,
      showRemark: false,
      groupList: [],
      ruleForm: {
        groupId: '',
        memo: ''
      },
      rules: {},
      tableData: [],
      leftList: [],
      selectedList: [],
      active: '',
      page: 1,
      total: 3,
      pageSize: 5,
      platformMap: {
        douyin: '抖音',
        kuaishou: '快手',
        xiaohongshu: '小红书',
        bilibili: '哔哩哔哩',
        shipinhao: '视频号',
        taobao: '淘宝'
      },
      current: {}
    }
  },
  computed: {
    currentGroup() {
      return this.leftList.find((item) => item.id === this.active) || {}
    }
  },
  watch: {
    currentGroup: {
      handler() {
        this.getListByGroup()
      },
      deep: true
    }
  },
  created() {},
  methods: {
    toBatch() {
      if (!this.selectedList.length) {
        this.$message.warning('请至少选择一条数据')
        return
      }
      this.isBatch = true
      this.showGroup = true
      console.log(this.selectedList)
    },
    async changeMemo() {
      if (this.current && this.current.id) {
        const res = await changeMemo({
          memo: this.ruleForm.memo || '',
          id: this.current.id.toString()
        })
        if (res.success) {
          this.$message.success('操作成功')
          this.showRemark = false
        } else {
          this.$message.error(res.message)
        }
      }
    },
    async putIntoGroup(value) {
      if (!this.isBatch) {
        if (this.current && this.current.id && value) {
          const res = await putIntoGroup({
            accountIds: this.current.id.toString(),
            groupId: value
          })
          if (res.success) {
            this.$message.success('操作成功')
            if (this.currentGroup && this.currentGroup.id) {
              this.getListByGroup()
            } else {
              this.tableData = []
            }
            this.showGroup = false
          } else {
            this.$message.error(res.message)
          }
        }
      } else {
        const res = await putIntoGroup({
          accountIds: this.selectedList
            .map((item) => item.id.toString())
            .join(','),
          groupId: value
        })
        if (res.success) {
          this.$message.success('操作成功')
          if (this.currentGroup && this.currentGroup.id) {
            this.getListByGroup()
          } else {
            this.tableData = []
          }
          this.showGroup = false
        } else {
          this.$message.error(res.message)
        }
      }
    },
    async getListByGroup() {
      console.log('getListByGroup', this.currentGroup)
      if (this.currentGroup) {
        const res = await getList({
          groupId: this.currentGroup.id
        })
        if (res.success) {
          this.tableData = res.result.records || []
          console.log('当前分组用户', this.tableData)
        }
      }
    },
    async open() {
      this.getGroupList()
      this.show = true
    },
    async getGroupList() {
      const res = await getGroupList()
      if (res.success) {
        this.leftList = [{ id: '', groupName: '全部' }, ...res.result.records]
        this.groupList = res.result.records
        this.getListByGroup()
      } else {
        this.$error(res.message)
      }
    },
    close() {
      this.show = false
    },
    handleSelectionChange(val) {
      this.selectedList = val
    },
    handleHide() {
      console.log('handleHide')
      this.show = false
    },
    handleGroup(item) {
      console.log('handleGroup', item)
      this.isBatch = false
      this.current = item
      this.showGroup = true
      this.show = false
    },
    handleRemark(item) {
      console.log('handleRemark')
      this.current = item
      this.show = false
      this.showRemark = true
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    async addGroup({ name }) {
      const res = await addGroup({ groupName: name })
      if (res.success) {
        this.$message.success('添加成功')
        this.getGroupList()
      } else {
        this.$message.error(res.message)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  display: grid;
  grid-template-columns: 180px 1fr;
  .avatar {
    width: 40px;
    height: 40px;
  }
  .left-box {
    margin-right: 30px;
    border-right: 1px solid rgba(128, 128, 128, 0.2);
    height: 340px;
    .left-wrapper {
      height: 300px;
      overflow-y: auto;
      margin-bottom: 10px;
    }
    // overflow-y: auto;
    .item {
      height: 44px;
      font-size: 14px;
      font-weight: 400;
      color: rgba(0, 0, 0, 1);
      cursor: pointer;
    }
    .item.active {
      background: rgba(0, 119, 255, 0.1);
      color: rgba(0, 119, 255, 1);
    }
    .link {
      font-size: 14px;
      font-weight: 400;
      color: rgba(0, 119, 255, 1);
    }
  }
}
</style>
